package com.gui.hospital.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gui.hospital.mapper.HospitalMapper;
import com.gui.hospital.model.Hospital;
import com.gui.hospital.service.HospitalService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 言叶
 * Date：2025/9/3
 */
@Service
@AllArgsConstructor

public class HospitalServiceImpl
        extends ServiceImpl<HospitalMapper, Hospital>
        implements HospitalService {
    private final HospitalMapper hospitalMapper;
    private final RedisTemplate redisTemplate;

    /*
     * 根据医院名称模糊查询医院信息
     * @param name 医院名称
     * @return 医院信息
     */
    @Override
    public List<Hospital> findByName(String name) {
        //如果name为空，则缓存返回所有医院信息
        if (name == null || name.isEmpty()) {
            //查询所有信息
            //先查询缓存
            Object registrationHospitalList = redisTemplate.boundValueOps("registration_hospitalList").get();
            if (registrationHospitalList != null) {
                return (List<Hospital>) registrationHospitalList;
            }
            else {
                List<Hospital> hospitalList = hospitalMapper.selectList(null);
                redisTemplate.boundValueOps("registration_hospitalList").set(hospitalList);
                return hospitalList;
            }
            //如果缓存中没有，则查询数据库，并将结果缓存
        }
        //根据医院名称模糊查询医院信息
            QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(Hospital::getChineseName, name);
        return hospitalMapper.selectList(queryWrapper);
    }

}
